#include "HttpServer.h"
#include "json.hpp"
#include "base64.h"

using namespace hv;

int main() {
    HttpService router;
    router.GET("/ping", [](HttpRequest* req, HttpResponse* resp) {
        return resp->String("pong");
    });

    router.GET("/data", [](HttpRequest* req, HttpResponse* resp) {
        static char data[] = "0123456789";
        return resp->Data(data, 10);
    });

    router.GET("/paths", [&router](HttpRequest* req, HttpResponse* resp) {
        return resp->Json(router.Paths());
    });

    router.GET("/get", [](HttpRequest* req, HttpResponse* resp) {
        resp->json["origin"] = req->client_addr.ip;
        resp->json["url"] = req->url;
        resp->json["args"] = req->query_params;
        resp->json["headers"] = req->headers;
        return 200;
    });

    router.POST("/echo", [](const HttpContextPtr& ctx)
{
    printf("%s\n", ctx->body().c_str());
    auto str = ctx->body();
    // 去掉固定化数据
    str =str.substr(15,str.length() - 15);
    char out[1024] = {0};
    // base64 解密（decode）
    int outlen = hv_base64_decode(str.c_str(),str.length(), (unsigned char*)out);
    printf("%s\n",out);
    // 发送处理结果
    return ctx->send(ctx->body(), ctx->type());
});

    HttpServer server(&router);
    server.setPort(8080);
    server.setThreadNum(4);
    server.run();
    return 0;
}